package com.g7.figmaapi.web.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.g7.figmaapi.common.result.WebResult;
import com.g7.figmaapi.web.api.enums.WebApiChannelEnum;
import com.g7.figmaapi.web.api.figma.domain.FigmaApiBo;
import com.g7.figmaapi.web.api.figma.domain.dto.*;
import com.g7.figmaapi.web.api.figma.domain.entity.FigmaFileVersion;
import com.g7.figmaapi.web.api.figma.domain.entity.FigmaUser;
import com.g7.figmaapi.web.api.figma.domain.req.*;
import com.g7.figmaapi.web.api.figma.service.FigmaApiService;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author: taoxiangqian
 * @explain: figmaAPI
 * @since: 2023/10/26 15:26
 */
@RestController
@RequestMapping(value = "/api/web/figmaApi")
public class FigmaApiController {
    @Resource
    private FigmaApiService figmaApiService;

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public ModelAndView test(HttpServletRequest request, ModelMap modelMap) {
        modelMap.addAttribute("channelType", WebApiChannelEnum.FIGMA.getCode());
        modelMap.addAttribute("code", 0);
        modelMap.addAttribute("data", 1);
        modelMap.addAttribute("msg", "成功");
        return new ModelAndView("pub/web/callBackResult", modelMap);
    }

    /**
     * 用户授权
     *
     * @param figmaReqForm figma通用请求实体
     * @return com.g7.figmaapi.common.result.WebResult<com.g7.figmaapi.web.api.figma.domain.FigmaApiBo>
     * @author taoxiangqian
     * @since 2023/11/12 18:29:47
     **/
    @RequestMapping(value = "/authorization", method = RequestMethod.POST)
    public WebResult<FigmaApiBo> authorization(@RequestBody FigmaReqForm figmaReqForm) {
        // todo 模拟获取登录用户id
        figmaReqForm.setUserId(getUserId());
        FigmaApiBo figmaApiBo = BeanUtil.copyProperties(figmaReqForm, FigmaApiBo.class);
        return WebResult.success(figmaApiService.authorization(figmaApiBo));
    }

    /**
     * figma用户授权回调
     *
     * @param figmaReqForm figma通用请求实体
     * @param modelMap     返回结果
     * @return org.springframework.web.servlet.ModelAndView
     * @author taoxiangqian
     * @since 2023/11/12 18:29:06
     **/
    @RequestMapping(value = "/authorizationCallBack", method = RequestMethod.GET)
    public ModelAndView authorizationCallBack(FigmaCallBackReqForm figmaReqForm, ModelMap modelMap) {
        FigmaCallBackBo figmaCallBackBo = BeanUtil.copyProperties(figmaReqForm, FigmaCallBackBo.class);
        WebResult<?> webResult = figmaApiService.authorizationCallBack(figmaCallBackBo);
        modelMap.addAttribute("channelType", WebApiChannelEnum.FIGMA.getCode());
        modelMap.addAttribute("code", webResult.getCode());
        modelMap.addAttribute("data", webResult.getData());
        modelMap.addAttribute("msg", webResult.getMsg());
        return new ModelAndView("pub/web/callBackResult", modelMap);
    }

    /**
     * 刷新用户授权
     *
     * @param figmaReqForm figma通用请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.Integer>
     * @author taoxiangqian
     * @since 2023/11/12 18:28:43
     **/
    @RequestMapping(value = "/refreshAuthorization", method = RequestMethod.POST)
    public WebResult<Integer> refreshAuthorization(FigmaReqForm figmaReqForm) {
        FigmaApiBo figmaApiBo = BeanUtil.copyProperties(figmaReqForm, FigmaApiBo.class);
        figmaApiBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.refreshAuthorization(figmaApiBo));
    }

    /**
     * 解除用户授权
     *
     * @param figmaReqForm figma通用请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.Integer>
     * @author taoxiangqian
     * @since 2023/11/12 18:34:19
     **/
    @RequestMapping(value = "/deauthorize", method = RequestMethod.POST)
    public WebResult<Integer> deauthorize(FigmaReqForm figmaReqForm) {
        FigmaApiBo figmaApiBo = BeanUtil.copyProperties(figmaReqForm, FigmaApiBo.class);
        figmaApiBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.deauthorize(figmaApiBo));
    }

    /**
     * 查询Figma用户信息
     *
     * @param figmaReqForm figma请求实体
     * @return com.g7.figmaapi.common.result.WebResult<com.g7.figmaapi.web.api.figma.domain.entity.FigmaUser>
     * @author taoxiangqian
     * @since 2023/11/12 18:27:52
     **/
    @RequestMapping(value = "/selUsers", method = RequestMethod.POST)
    public WebResult<FigmaUser> selUsers(FigmaReqForm figmaReqForm) {
        figmaReqForm.setUserId(getUserId());
        FigmaApiBo figmaApiBo = BeanUtil.copyProperties(figmaReqForm, FigmaApiBo.class);
        return WebResult.success(figmaApiService.selUsers(figmaApiBo));
    }

    /**
     * 查询文件版本信息
     *
     * @param figmaReqForm 文件请求实体
     * @return com.g7.figmaapi.common.result.WebResult<com.g7.figmaapi.web.api.figma.domain.entity.FigmaFileVersion>
     * @author taoxiangqian
     * @since 2023/11/12 18:27:26
     **/
    @RequestMapping(value = "/selFileVersion", method = RequestMethod.POST)
    public WebResult<FigmaFileVersion> selFileVersion(@RequestBody FigmaReqForm figmaReqForm) {
        figmaReqForm.setUserId(getUserId());
        FigmaFileVersionBo figmaFileVersionBo = BeanUtil.copyProperties(figmaReqForm, FigmaFileVersionBo.class);
        return WebResult.success(figmaApiService.selFileVersion(figmaFileVersionBo));
    }

    /**
     * 查询文件信息
     *
     * @param figmaFileReqForm 文件请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.String>
     * @author taoxiangqian
     * @since 2023/11/12 18:27:07
     **/
    @RequestMapping(value = "/selFile", method = RequestMethod.POST)
    public WebResult<String> selFile(@RequestBody FigmaFileReqForm figmaFileReqForm) {
        FigmaFileBo figmaFileBo = BeanUtil.copyProperties(figmaFileReqForm, FigmaFileBo.class);
        figmaFileBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.selFile(figmaFileBo));
    }

    /**
     * 查询文件节点信息
     *
     * @param figmaFileNodesReqForm 文件节点请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.String>
     * @author taoxiangqian
     * @since 2023/11/12 18:26:41
     **/
    @RequestMapping(value = "/selFileNodes", method = RequestMethod.POST)
    public WebResult<String> selFileNodes(@RequestBody FigmaFileNodesReqForm figmaFileNodesReqForm) {
        FigmaFileNodesBo figmaFileNodesBo = BeanUtil.copyProperties(figmaFileNodesReqForm, FigmaFileNodesBo.class);
        figmaFileNodesBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.selFileNodes(figmaFileNodesBo));
    }

    /**
     * 查询图像
     *
     * @param figmaImageReqForm 图像请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.String>
     * @author taoxiangqian
     * @since 2023/11/12 18:25:59
     **/
    @RequestMapping(value = "/selImage", method = RequestMethod.POST)
    public WebResult<String> selImage(@RequestBody FigmaImageReqForm figmaImageReqForm) {
        FigmaImageBo figmaImageBo = BeanUtil.copyProperties(figmaImageReqForm, FigmaImageBo.class);
        figmaImageBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.selImage(figmaImageBo));
    }

    /**
     * 获取文档中图像填充中所有图像的下载链接
     *
     * @param figmaImageReqForm 图像填充请求实体
     * @return com.g7.figmaapi.common.result.WebResult<java.lang.String>
     * @author taoxiangqian
     * @since 2023/11/12 18:25:17
     **/
    @RequestMapping(value = "/selImageFills", method = RequestMethod.POST)
    public WebResult<String> selImageFills(@RequestBody FigmaImageFillsReqForm figmaImageReqForm) {
        FigmaImageFillsBo figmaImageFillsBo = BeanUtil.copyProperties(figmaImageReqForm, FigmaImageFillsBo.class);
        figmaImageFillsBo.setUserId(getUserId());
        return WebResult.success(figmaApiService.selImageFills(figmaImageFillsBo));
    }

    private Long getUserId() {
        return 1L;
    }
}
